//
//  BHStadiumSuitTypeCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/1.
//

import UIKit

class BHStadiumSuitTypeCell: UITableViewCell {
    var didChoseSuitBlock:((Int) -> Void)?
    
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        let view = UICollectionView.buildCollectionView(layout: layout)
        view.register(BHStadiumProSuitTypeCell.self, forCellWithReuseIdentifier: "BHStadiumProSuitTypeCell")
        view.backgroundColor = .white
        view.delegate = self
        view.dataSource = self
        return view
    }()
    
    var stadiumProArray: [BHStadiumDetailGoodModel] = [] {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    var choseIndex: Int = 0 {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(self.collectionView)
        self.selectionStyle = .none
        self.collectionView.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(16)
            make?.top.mas_equalTo()(5)
            make?.height.mas_equalTo()(30)
            make?.right.equalTo()(self)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    static var height :CGFloat {
        get {
            return 40.0
        }
    }
    
}
extension BHStadiumSuitTypeCell: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.stadiumProArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if self.stadiumProArray.count == 0 {
            return .zero
        }
        let model = self.stadiumProArray[indexPath.row]
        let  width = model.name.count * 12 + 20
        return CGSize(width: width, height: 22)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if self.stadiumProArray.count == 0 {
            return .zero
        }
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if self.stadiumProArray.count == 0 {
            return 0
        }
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        if self.stadiumProArray.count == 0 {
            return 0
        }
        return 10
    }
    
}
 
extension BHStadiumSuitTypeCell: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHStadiumProSuitTypeCell", for: indexPath) as! BHStadiumProSuitTypeCell
        let model = self.stadiumProArray[indexPath.row]
        cell.name_label.text = model.name
        cell.chose = indexPath.row == self.choseIndex
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.didChoseSuitBlock?(indexPath.row)
        self.choseIndex = indexPath.row
        collectionView.reloadData()
    }
}
